package com.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.pojo.Category;
import com.reggie.pojo.Dish;
import com.reggie.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
@Transactional
public class CategoryController {
    @Autowired
    private CategoryServiceImpl service;
    //添加菜品
    @PostMapping
    public R<String> add(@RequestBody Category category){
        System.out.println("添加菜品服务器");
        Boolean save = service.save(category);
        if(save){
            return R.success("添加成功！");
        }else {
            return R.error("添加失败");
        }
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<Category>> SelectByPage(Integer page,Integer pageSize,Integer sort){
        Page<Category> categoryPage = service.selectByPage(page, pageSize,sort);
        return R.success(categoryPage);
    }

    //删除功能
    @DeleteMapping()
    public R<String> DeleteById(Long ids){
        System.out.println("删除服务器");
        if(service.DeleteById(ids) >0){
            return R.success("删除成功！");
        }else {
            return R.error("删除失败！");
        }
    }

    @PutMapping
    //修改菜品
    public R<String> updateCategory(@RequestBody Category category){
        System.out.println(category);
        if(service.UpdateCategory(category) > 0){
            return R.success("修改成功！");
        }else {
            return R.error("修改失败！");
        }
    }


    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        //把菜品数据返回给前端
        System.out.println(type);
        if(type == null){
            return R.success(service.selectAll());
        }
        System.out.println(service.SelectByType(type));
        return R.success(service.SelectByType(type));
    }
}
